//
//  SetupViewController.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/24.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "SetupViewController.h"
#import <Masonry/Masonry.h>
#import "ResponseManager.h"
#import "UserModel.h"
#import "ResponseManager.h"
#import "ResponseManager+ModifyUserInfo.h"
#import "MemberViewController.h"
#import "ChangePassWordViewController.h"
#import "FBProgressView.h"
#import <SDWebImage/SDImageCache.h>
#import <EMSDK.h>

@interface SetupViewController ()<UITextFieldDelegate>
{
    UITextField *nickName;
    UITextField *password;
    UITextField *mobile;
    UITextField *clear;
    UIButton *exitButton;
    
}

@end

@implementation SetupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBCOLOR(241.0f, 241.0f, 241.0f, 1);
    [self setupBackItem];
    
    [self setUpView];
    
   
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}

- (void)setUpView {
    
    nickName  = [self setTextFieldTex:@"昵称" image:nil];
    [self.view addSubview:nickName];
    
    [nickName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(74);
        make.height.mas_equalTo(50);
        
    }];
    nickName.delegate = self;
    nickName.text = self.name;
    
    
    password = [self setTextFieldTex:@"密码" image:@"个人中心-right"];
    [self.view addSubview:password];
    
    [password mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(nickName.mas_bottom).with.offset(1);
        make.height.mas_equalTo(50);
        
    }];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [userDefaults objectForKey:@"userInfo"];
    
    password.text = dic[@"password"];
    password.secureTextEntry = YES;
    
    UIButton *passButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [password addSubview:passButton];
    
    [passButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.height.equalTo(password);
    }];
    
    passButton.tag = 500;
//    [passButton setBackgroundColor:[UIColor redColor]];
//    password.userInteractionEnabled = NO;
    
    [passButton addTarget:self action:@selector(pusNextView:) forControlEvents:UIControlEventTouchUpInside];

    

    
    
    mobile = [self setTextFieldTex:@"手机号" image:nil];
    [self.view addSubview:mobile];
    
    [mobile mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(password.mas_bottom).with.offset(1);
        make.height.mas_equalTo(50);
        
    }];
    mobile.text = dic[@"username"];
    UIButton *mobileButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [mobile addSubview:mobileButton];
    
    [mobileButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.height.equalTo(mobile);
    }];
    
    mobileButton.tag = 501;
    
    

    
    
    clear = [self setTextFieldTex:@"清理缓存" image:@"钱包-清除缓存"];
    [self.view addSubview:clear];
    
    [clear mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(mobile.mas_bottom).with.offset(10);
        make.height.mas_equalTo(50);
        
    }];
    
    UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [clear addSubview:clearButton];
    
    [clearButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.height.equalTo(clear);
        
    }];
    clearButton.tag = 502;
    [clearButton addTarget:self action:@selector(pusNextView:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    exitButton = [[UIButton alloc]init];
    exitButton.backgroundColor = BLUE_COLOR;
    [exitButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [exitButton setTitleColor:RGBCOLOR(205.0f, 205.0f, 205.0f, 1) forState:UIControlStateHighlighted];
    exitButton.titleLabel.textColor = [UIColor whiteColor];
    exitButton.titleLabel.font = [UIFont systemFontOfSize:15];
    exitButton.layer.cornerRadius = 5;
    [self.view addSubview:exitButton];
    
    [exitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(clear.mas_bottom).with.offset(30);
        make.height.mas_equalTo(50);
        
    }];
    
    [exitButton addTarget:self action:@selector(extitButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)extitButtonTaped:(UIButton *)sender {
    
    
    UserModel *model = [UserModel sharedUserModel];
 [FBProgressView showWithMessage:nil];
    [ResponseManager startGetRequestWithURLString:klogonOutURL parameters:nil completionHandler:^(FBResponse *response) {
        if (response.success) {
            model.islogin = NO;
             [FBProgressView hide];
            [[EMClient sharedClient] asyncLogout:YES success:^{
                PLLOG(@"登出");
                UserModel *user = [UserModel sharedUserModel];
                user.islogin = NO;
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userInfo"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                [self.navigationController popViewControllerAnimated:YES];
            } failure:^(EMError *aError) {
                PLLOG(@"登出失败");
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }else {
            NSLog(@"%@",response.resultDesc);
        }
        
    }];
    
    
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    self.title = @"修改昵称";
   [self setupRightItemWithText:@"保存" action:@selector(saveButtonTapde)];
    
    return YES;
}

- (void)saveButtonTapde {
    [FBProgressView showWithMessage:nil];

    [ResponseManager amendUserInfoNiceName:nickName.text pass:nil completionHandler:^(FBResponse *response) {
        if (response.success) {
            [FBProgressView hide];
 [self addAlertControllerWithTitle:@"保存成功" message:nil actionNum:1 style:UIAlertControllerStyleAlert handler:nil];        }
        
    }];
    
    
}


- (void)pusNextView:(UIButton *)sender {
    NSInteger index = sender.tag - 500;
    
    if (index == 0) {
        ChangePassWordViewController *changePassWordVC = [[ChangePassWordViewController alloc]init];
        [self.navigationController pushViewController:changePassWordVC animated:YES];
        
    }
    
    if (index == 2) {
        
        SDImageCache *sdimage = [[SDImageCache alloc]init];
        [sdimage clearMemory];
        
    }
    
    
}




- (UITextField *)setTextFieldTex:(NSString *)text image:(NSString *)image {
    
    UITextField *textFiled = [[UITextField alloc]init];
    textFiled.borderStyle = UITextBorderStyleNone;
    textFiled.backgroundColor = [UIColor whiteColor];
    
    UIView *leftview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 50)];
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 30)];
    lable.text = text;
    lable.font = [UIFont systemFontOfSize:15];
    
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 15, 20, 20)];
    UIView *rightview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 50)];
    imageview.image = [UIImage imageNamed:image];
    textFiled.rightView = rightview;
    [textFiled setRightViewMode:UITextFieldViewModeAlways];
    [rightview addSubview:imageview];
    
    textFiled.leftView = leftview;
    [textFiled setLeftViewMode:UITextFieldViewModeAlways];
    [leftview addSubview:lable];
    
    return textFiled;
    
}




@end
